var httpUtils = require('../../utils/httpUtils.js');
var utils = require('../../utils/util.js');
var app = getApp();

Page({
  data: {
    hiddenModalInput: false,
    nameInput: '',
    idCardInput: ''
  },
  onLoad: function(option) {
    var nameInput = option.name;
    var idCardInput = option.idCard;
    this.setData({
      'idCardInput': idCardInput,
      'nameInput': nameInput
    });
  },
  cancel: function() {
    this.setData({
      hiddenModalInput: true
    })
  },
  confirm: function() {
    console.log('这是啥');
    console.log(wx.getStorageSync('userInfo'));
    var name = this.data.nameInput;
    var idCard = this.data.idCardInput;


    if (!utils.isIdCardNo(idCard)) {
      wx.showToast({
        title: '请设置正确的身份证号码.',
        icon:'none',
        duration: 2000
      });
      return;
    }
    //吧框隐藏掉
    this.setData({
      hiddenModalInput: true
    });
   
    var _this = this;
    var url = app.globalData.interfaceUrl + '/worker.do?method=updateWorkerInfo';
    console.log(url);
    httpUtils.post(url, {
      'workerName': name,
      'idCard': idCard
    }, function(res) {
      var resp = res.data;
      var userInfo = wx.getStorageSync('userInfo');
      userInfo.workerName = name;
      userInfo.idCard = idCard;
      wx.setStorageSync('userInfo', userInfo);
      console.log(JSON.stringify(userInfo));
      wx.switchTab({
        url: '../personal/personal',
        success: function(e) {
          var page = getCurrentPages().pop();
          if (!utils.isNull(page)) {
            page.onLoad();
          }
        }
      });
      wx.showToast({
        title: '操作成功',
        icon: 'succes',
        duration: 1000,
        mask: true
      })
    });
  },

  //获取用户输入的名字
  nameInput: function(e) {
    console.log(e);
    this.setData({
      nameInput: e.detail.value
    })
  },
  //获取用户输入的身份证号
  idCardInput: function(e) {
    console.log(e);
    this.setData({
      idCardInput: e.detail.value
    })
  }
})